package com.lris.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.lris.constant.LoginTokenTimeConstant;
import com.lris.constant.MessageConstant;
import com.lris.dto.UsernameLoginDto;
import com.lris.pojo.User;
import com.lris.result.PageResult;
import com.lris.result.Result;
import com.lris.service.UserService;
import com.lris.utils.VerifyUtil;
import com.lris.vo.CodeVo;
import com.lris.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 前端控制器
 *
 * @author lris
 * @since 2023-09-10 01:55:40
 */

@RestController
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户模块")
@RequestMapping("/lris/user")
public class UserController {

    private final UserService userService;

    private final RedisTemplate<String, String> redisTemplate;


    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public Result<UserVo> login(@RequestBody UsernameLoginDto usernameLoginDto) {

        log.info("登录接口接收参数:{}", usernameLoginDto);

        User user = userService.login(usernameLoginDto);

        if (null == usernameLoginDto.getIsme()) {
            StpUtil.login(user.getId(), LoginTokenTimeConstant.LONG_TIME);//        拿到当前isme 的值 做判断 如果是 true  就把session 设置过期时间为  30天
        } else {
            StpUtil.login(user.getId(), LoginTokenTimeConstant.SHORT_TIME);//        false  就把session 设置过期时间为  1天
        }
        String token = StpUtil.getTokenValue();
        UserVo userVo = UserVo.builder()
                .id(user.getId())
                .role(user.getRole())
                .phone(user.getPhone())
                .gender(user.getGender())
                .nickname(user.getNickname())
                .username(user.getUsername())
                .token(token)
                .createTime(user.getCreateTime())
                .headImg(user.getHeadImg())
                .build();
        return Result.success(userVo);
    }

    @PostMapping("/logout")
    @Operation(summary = "退出登录接口")
    public Result logout(Long id) {
        log.info("用户id:{}退出登录", id);
        StpUtil.login(id);
        return Result.success(MessageConstant.LOGOUT_SUCCESS);
    }

    @GetMapping("/code")
    @Operation(summary = "发送验证码")
    public Result code() {
        Object[] objs = VerifyUtil.createImage();
        String randomStr = (String) objs[0];
        String codeId = IdUtil.randomUUID();
        log.info(codeId);
        redisTemplate.opsForValue().set(codeId, randomStr);
        redisTemplate.expire(codeId, 5, TimeUnit.MINUTES);
        log.info("randomStr  result: " + randomStr);
        CodeVo build = CodeVo.builder().codeUrl((byte[]) objs[1]).codeId(codeId).build();
        return Result.success(build);
    }

    @GetMapping("/find")
    @Operation(summary = "查找用户")
    public Result<PageResult> findUserFriend(Integer page,Integer pageSize , String search) {
        if(null==search||"".equals(search)){
            return Result.error("英雄名"+MessageConstant.FILED_NOT_NULL);
        }
        PageResult pageResult=userService.findUserFriend(page,pageSize,search);
            return Result.success(pageResult);
    }

}
